package com.xiaoxin.experience.service.tree;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxin.experience.api.tree.model.DirectoryInfo;
import com.xiaoxin.experience.service.tree.entity.DirectoryEntity;
import com.xiaoxin.experience.service.tree.mapper.DirectoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaoxin
 */
@Service
public class DirectoryService extends ServiceImpl<DirectoryMapper, DirectoryEntity>
{

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public List<DirectoryInfo> getDirectoryList()
    {
        return baseMapper.selectList(lambdaQuery().getWrapper()).stream().map(DirectoryEntity::toInfo).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(DirectoryInfo directoryInfo)
    {
        if (directoryInfo == null)
        {
            return false;
        }
        return saveOrUpdate(new DirectoryEntity(directoryInfo));
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDirectory(Integer id)
    {
        return removeById(id);
    }
}
